package cn.spring.service.impl;

import cn.spring.entity.Bill;
import cn.spring.entity.Provider;
import cn.spring.entity.Role;
import cn.spring.entity.User;
import cn.spring.mapper.UserMapper;
import cn.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
    //声明UserMapper接口引用
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Provider> findUsersWithConditions() {
        try {
            //调用DAO方法实现查询
            return userMapper.getProviderList();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Provider> getProviderLike(String proName) {
        try {
            return userMapper.getProviderLike(proName);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Bill> getBill(Map<String, Object> map) {
        try {
            return userMapper.getBill(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean add(Provider provider) {
        boolean result = false;
        try {
            if (userMapper.add(provider) == 1){
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateProvider(Provider provider) {
        boolean result = false;
        try {
            if (userMapper.updateProvider(provider) == 1){
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteProvider(int id) {
        boolean result = false;
        try {
            if (userMapper.deleteProvider(id) == 1){
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Role> getRoleList() {
        try {
            //调用DAO方法实现查询
            return userMapper.getRoleList();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean insertRole(Role role) {
        boolean result = false;
        try {
            if (userMapper.insertRole(role) == 1){
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }


    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper usermapper) {
        this.userMapper = usermapper;
    }
}
